package com.cleaningcloud.cld.controller.mini;

import com.cleaningcloud.cld.api.domain.CldStaff;
import com.cleaningcloud.cld.api.domain.Vo.CldStaffDto;
import com.cleaningcloud.cld.domain.*;
import com.cleaningcloud.cld.domain.dto.*;
import com.cleaningcloud.cld.domain.mini.addSurveryDto;
import com.cleaningcloud.cld.domain.vo.*;
import com.cleaningcloud.cld.service.*;
import com.cleaningcloud.cld.service.mini.IMiniPersonnelService;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 人事管理 - 小程序接口
 */
@RestController
@RequestMapping("/mini/personnel")
public class MiniPersonnelController extends BaseController {

    @Autowired
    private IMiniPersonnelService personnelService;

    @Autowired
    private ICldTrainReceiveService cldTrainReceiveService;

    @Autowired
    private ICldInsuranceDetailsService insuranceDetailsService;

    @Autowired
    private ICldRecruitPlanService recruitPlanService;

    @Autowired
    private ICldDegreetitleService cldDegreetitleService;

    @Autowired
    private ICldStaffService cldStaffService;

    @Autowired
    private ICldProjectPostService cldProjectPostService;

    @Autowired
    private ICldStaffComplaintService cldStaffComplaintService;


    @Operation(description = "新增员工")
    @PostMapping("/addStaff")
    public AjaxResult addStaff(@RequestBody CldStaffDto cldStaff) {
        if (!cldStaffService.checkUserPhoneUnique(cldStaff)) {
            return error("保存员工失败，该手机号已存在");
        }
        if (!cldStaffService.checkUserIdCardUnique(cldStaff)) {
            return error("保存员工失败，该身份证已存在");
        }
        return toAjax(personnelService.insertStaff(cldStaff));
    }

    @Operation(description = "获取员工信息详情")
    @GetMapping("/getStaffInfo/{staffId}")
    public AjaxResult getStaffInfo(@PathVariable Long staffId) {
        return success(personnelService.getStaffInfo(staffId));
    }

    @Operation(description = "员工入岗")
    @PostMapping("/addStaffPost")
    public AjaxResult addStaffPost(@RequestBody CldProjectPost cldProjectPost) {
        return toAjax(cldProjectPostService.updateCldProjectPost(cldProjectPost));
    }

    @Operation(description = "查询待办事项")
    @GetMapping("/getStaffTodo/{projectPostId}")
    public AjaxResult getStaffTodo(@PathVariable Long projectPostId) {
        return success(personnelService.getStaffTodo(projectPostId,SecurityUtils.getUserId()));
    }

    @Operation(description = "修改员工信息")
    @PutMapping("/editStaff")
    public AjaxResult editStaff(@RequestBody CldStaffDto cldStaff) {
        if (!cldStaffService.checkUserPhoneUnique(cldStaff)) {
            return error("修改员工失败，该手机号已存在");
        }
        if (!cldStaffService.checkUserIdCardUnique(cldStaff)) {
            return error("修改员工失败，该身份证已存在");
        }
        return toAjax(personnelService.updateCldStaff(cldStaff));
    }


    @Operation(description = "修改密码")
    @PutMapping("/updatePassword")
    public AjaxResult updatePassword(String password, String newPassword) {
        return toAjax(personnelService.updatePassword(password, newPassword));
    }

    @Operation(description = "找回密码")
    @PutMapping("/recoverPassword")
    public AjaxResult recoverPassword(String phone, String password, String code) {
        return toAjax(personnelService.recoverPassword(phone, password, code));
    }

    /**
     * @Author Shaw
     * @Description 获取员工信息(小程序-在职人员列表)
     * @date: 2025-05-28 16:00
     * @param: //TODO
     */
    @Operation(description = "获取员工信息")
    @GetMapping("/getGroupStaffList")
    public TableDataInfo getGroupStaffList(CldStaffDto cldStaffDto) {
        return getMiniDataTable(personnelService.getGroupStaffList(cldStaffDto));
    }

    @Operation(description = "查询员工列表")
    @GetMapping("/getStaffList")
    public TableDataInfo getStaffList(CldStaffDto cldStaffDto) {
        startPage();
        List<CldStaffVo> list = personnelService.getStaffList(cldStaffDto);
        return getMiniDataTable(list);
    }

    @Operation(description = "员工无效/入职审核/离职审核")
    @PutMapping("/auditStaff")
    public AjaxResult auditStaff(@RequestBody CldStaff cldStaff) {
        return toAjax(personnelService.auditStaff(cldStaff));
    }

    @Operation(description = "员工离职申请")
    @PutMapping("/staffResignation")
    public AjaxResult staffResignation(@RequestBody CldStaff cldStaff) {
        return toAjax(personnelService.staffResignation(cldStaff));
    }


    @Operation(description = "新增保险理赔")
    @PostMapping("/addInsurance")
    public AjaxResult addInsurance(@RequestBody CldInsuranceDetails cldInsuranceDetails) {
        return toAjax(insuranceDetailsService.insertCldInsuranceDetails(cldInsuranceDetails));
    }


    @Operation(description = "查询理赔列表")
    @GetMapping("/getClaimsList")
    public TableDataInfo getClaimsList(CldInsuranceDetailsDto cldInsuranceDetailsDto) {
        startPage();
        List<CldInsuranceDetailsVo> list = insuranceDetailsService.selectCldInsuranceDetailsList(cldInsuranceDetailsDto);
        return getMiniDataTable(list);
    }


    @Operation(description = "修改保险理赔")
    @PutMapping("/editInsurance")
    public AjaxResult editInsurance(@RequestBody CldInsuranceDetails cldInsuranceDetails) {
        return toAjax(insuranceDetailsService.updateCldInsuranceDetails(cldInsuranceDetails));
    }

    @Operation(description = "查看理赔详情")
    @GetMapping("/getInsuranceInfo/{insuranceDetailsId}")
    public AjaxResult getInsuranceInfo(@PathVariable Long insuranceDetailsId) {
        return success(insuranceDetailsService.selectCldInsuranceDetailsByInsuranceDetailsId(insuranceDetailsId));
    }


    @Operation(description = "查询培训计划列表")
    @GetMapping("/getTrainingList")
    public TableDataInfo getTrainingList(CldTrainReceiveDto cldTrainReceiveDto) {
        startPage();
        List<CldTrainReceiveVo> list = cldTrainReceiveService.selectCldTrainReceiveList(cldTrainReceiveDto);
        return getMiniDataTable(list);
    }

    @Operation(description = "查询培训计划详情")
    @GetMapping("/getTrainingInfo/{trainReceiveId}")
    public AjaxResult getTrainingInfo(@PathVariable Long trainReceiveId) {
        return success(cldTrainReceiveService.selectCldTrainReceiveByTrainReceiveId(trainReceiveId));
    }


    @Operation(description = "培训计划审核员反馈/员工确认")
    @PutMapping("/editTrainingInfo")
    public AjaxResult getTrainingInfo(@RequestBody CldTrainReceiveDto cldTrainReceiveDto) {
        return toAjax(cldTrainReceiveService.updateCldTrainReceive(cldTrainReceiveDto));
    }


    @Operation(description = "查询招聘计划列表")
    @GetMapping("/getRecruitmentPlanList")
    public TableDataInfo getRecruitmentPlanList(CldRecruitPlanDto cldRecruitPlanDto) {
        startPage();
        List<CldRecruitPlanVo> list = recruitPlanService.selectCldRecruitPlanList(cldRecruitPlanDto);
        return getMiniDataTable(list);
    }

    @Operation(description = "查询招聘计划详情")
    @GetMapping("/getRecruitmentPlanInfo/{recruitPlanId}")
    public AjaxResult getRecruitmentPlanInfo(@PathVariable Long recruitPlanId) {
        return success(recruitPlanService.selectCldRecruitPlanByRecruitPlanId(recruitPlanId));
    }


    @Operation(description = "创建招聘计划")
    @PostMapping("/addRecruitmentPlan")
    public AjaxResult addRecruitmentPlan(@RequestBody CldRecruitPlanDto cldRecruitPlanDto) {
        return toAjax(recruitPlanService.insertCldRecruitPlan(cldRecruitPlanDto));
    }

    @Operation(description = "查询通知公告列表")
    @GetMapping("/getNoticeList")
    public TableDataInfo getNoticeList(CldNoticeConfirm cldNoticeConfirm) {
        startPage();
        List<CldNoticeConfirmVo> list = personnelService.getNoticeList(cldNoticeConfirm);
        return getMiniDataTable(list);
    }

    @Operation(description = "通知公共确认")
    @PutMapping("/noticeConfirm")
    public AjaxResult noticeConfirm(@RequestBody CldNoticeConfirm cldNoticeConfirm) {
        return toAjax(personnelService.noticeConfirm(cldNoticeConfirm));
    }

    @Operation(description = "通知公共详情")
    @GetMapping("/getNoticeDetails/{noticeConfirmId}")
    public AjaxResult getNoticeDetails(@PathVariable Long noticeConfirmId) {
        return success(personnelService.getNoticeDetails(noticeConfirmId));
    }

    @Operation(description = "查询调查问卷列表")
    @GetMapping("/getSurveyList")
    public TableDataInfo getSurveyList(CldDegreetitleDto cldDegreetitleDto) {
        cldDegreetitleDto.setPersonId(SecurityUtils.getUserId());
        cldDegreetitleDto.setSendStatus(1L);
        startPage();
        List<CldDegreetitleVo> list = cldDegreetitleService.selectCldDegreetitleList(cldDegreetitleDto);
        return getMiniDataTable(list);
    }

    @Operation(description = "调查问卷详情")
    @GetMapping("/selectSurveryDetails")
    public AjaxResult selectSurveryDetails(CldDegreetitleDto cldDegreetitleDto) {
        return success(cldDegreetitleService.selectSurveryDetails(cldDegreetitleDto));
    }

    @Operation(description = "满意度提交")
    @PostMapping("/addSurvery")
    public AjaxResult addSurvery(@RequestBody addSurveryDto addSurveryDto) {
        return toAjax(personnelService.addSurvery(addSurveryDto));
    }


    @Operation(description = "员工投诉列表查看")
    @GetMapping("/getStaffComplaintList")
    public TableDataInfo getStaffComplaintList(CldStaffComplaintDto cldStaffComplaint) {
        cldStaffComplaint.setStaffId(SecurityUtils.getUserId());
        startPage();
        List<CldStaffComplaintVo> list = cldStaffComplaintService.selectCldStaffComplaintList(cldStaffComplaint);
        return getMiniDataTable(list);
    }

    @Operation(description = "员工投诉详情查看")
    @GetMapping("/getStaffComplaintDetails/{staffComplaintId}")
    public AjaxResult getStaffComplaintDetails(@PathVariable Long staffComplaintId) {
        return success(cldStaffComplaintService.selectCldStaffComplaintByStaffComplaintId(staffComplaintId));
    }

    @Operation(description = "新增员工投诉")
    @PostMapping("/addStaffComplaint")
    public AjaxResult addStaffComplaint(@RequestBody CldStaffComplaintDto cldStaffComplaintDto) {
        return toAjax(cldStaffComplaintService.insertCldStaffComplaint(cldStaffComplaintDto));
    }
}
